﻿using System.Collections.Generic;
using System.Linq;
using DTO;

namespace DAO
{
    public class LichSuTinRaoVatViPhamDAO
    {
        public static List<LichSuTinRaoVatViPhamDTO> getAll()
        {
            List<LichSuTinRaoVatViPhamDTO> result = new List<LichSuTinRaoVatViPhamDTO>();
            RaoVatDataContext dbRaoVat = new RaoVatDataContext();

            var query = dbRaoVat.LICHSUTINRAOVATVIPHAMs.Where(dm => dm.deleted.Equals(0)).ToList();

            foreach (var ds in query)
            {
                LichSuTinRaoVatViPhamDTO dto = new LichSuTinRaoVatViPhamDTO();

                dto.MaLichSuTinRaoVatViPham = ds.MaLichSuTinRaoVatViPham;
                dto.MaTinRaoVatViPham = ds.MaTinRaoVatViPham.Value;
                dto.MaNguoiDungBaoCaoViPham = ds.MaNguoiDungBaoCaoViPham.Value;
                dto.ThoiGianBaoCaoViPham = ds.ThoiGianBaoCaoViPham.Value;
                dto.LyDo = ds.LyDo;

                result.Add(dto);
            }

            return result;
        }

        public static LichSuTinRaoVatViPhamDTO getAlongCode(int maLSTRV)
        {
            LichSuTinRaoVatViPhamDTO result = new LichSuTinRaoVatViPhamDTO();
            RaoVatDataContext dbRaoVat = new RaoVatDataContext();

            var query = dbRaoVat.LICHSUTINRAOVATVIPHAMs.Where(dm => dm.deleted.Equals(0) &&
                dm.MaLichSuTinRaoVatViPham == maLSTRV).Single();

            result.MaLichSuTinRaoVatViPham = query.MaLichSuTinRaoVatViPham;
            result.MaTinRaoVatViPham = query.MaTinRaoVatViPham.Value;
            result.MaNguoiDungBaoCaoViPham = query.MaNguoiDungBaoCaoViPham.Value;
            result.ThoiGianBaoCaoViPham = query.ThoiGianBaoCaoViPham.Value;
            result.LyDo = query.LyDo;

            return result;
        }

        public static void Insert(LichSuTinRaoVatViPhamDTO _insert)
        {
            RaoVatDataContext dbRaoVat = new RaoVatDataContext();

            LICHSUTINRAOVATVIPHAM lichsu = new LICHSUTINRAOVATVIPHAM();
            lichsu.MaTinRaoVatViPham = _insert.MaTinRaoVatViPham;
            lichsu.MaNguoiDungBaoCaoViPham = _insert.MaNguoiDungBaoCaoViPham;
            lichsu.ThoiGianBaoCaoViPham = _insert.ThoiGianBaoCaoViPham;
            lichsu.LyDo = _insert.LyDo;
            lichsu.deleted = _insert.Deleted;            

            dbRaoVat.LICHSUTINRAOVATVIPHAMs.InsertOnSubmit(lichsu);
            dbRaoVat.SubmitChanges();
        }

        public static void Delete(int maLSTRV)
        {
            RaoVatDataContext dbRaoVat = new RaoVatDataContext();

            LICHSUTINRAOVATVIPHAM _delete = (from n in dbRaoVat.LICHSUTINRAOVATVIPHAMs
                                             where n.MaLichSuTinRaoVatViPham == maLSTRV && n.deleted == 0
                                             select n).Single();

            _delete.deleted = 1;
            dbRaoVat.SubmitChanges();
        }

        public static void Update(LichSuTinRaoVatViPhamDTO dto)
        {
            RaoVatDataContext dbRaoVat = new RaoVatDataContext();

            LICHSUTINRAOVATVIPHAM _update = (from n in dbRaoVat.LICHSUTINRAOVATVIPHAMs
                                             where n.MaLichSuTinRaoVatViPham == dto.MaLichSuTinRaoVatViPham && n.deleted == 0
                                             select n).Single();

            _update.MaTinRaoVatViPham = dto.MaTinRaoVatViPham;
            _update.MaNguoiDungBaoCaoViPham = dto.MaNguoiDungBaoCaoViPham;
            _update.ThoiGianBaoCaoViPham = dto.ThoiGianBaoCaoViPham;
            _update.LyDo = dto.LyDo;

            dbRaoVat.SubmitChanges();
        }
    }
}
